package com.itheima.gongjulei;

import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/*
同步屏障
CyclicBarrier
 */
public class Demo02 {


    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(8);
        for (int i = 0; i < 8; i++) {
            YDY ydy = new YDY(cyclicBarrier);
            Thread thread = new Thread(ydy);
            thread.setName(i+"号运动员");
            thread.start();
        }
    }
}

class  YDY implements Runnable{
    private  CyclicBarrier cyclicBarrier;

    public YDY(CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        System.out.println(name+"已就绪");
        try {
            cyclicBarrier.await();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //开始跑步
        try {
            Thread.sleep(new Random().nextInt(1000)+9000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(name+"到达了重点");
    }
}
